package seismosurfer.gui.panel;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import seismosurfer.data.MagData;
import seismosurfer.data.QuakeData;
import seismosurfer.http.HttpGateway;
import seismosurfer.util.TimeUtil;

/**
 * This AttributesPanel shows the attributes of the 
 * quake layer objects.
 *
 */
public class QuakeAttributesPanel extends AttributesPanel {
    
    private static final long serialVersionUID = 4486373144095769578L;

    protected java.util.List mags = new ArrayList(2);

    protected JTable masterTable;

    protected String masterTableTitle = "Earthquake attributes";

    protected JTable detailsTable;

    protected String masterTableHeaders[] = { CATALOG_NAME,

    QUAKE_ID, DATETIME, LATITUDE, LONGITUDE, DEPTH, AGENCY, FECODE, HAS_MACRO,
            HAS_INFO };

    protected String detailHeaders[] = { MAGNITUDE_ID, MAGNITUDE, MAG_TYPE,
            CALCULATED, MAG_AGENCY };

    public QuakeAttributesPanel(String name) {
        super(name, false);
        createPanel();
    }

    public void setAttributes(Object data) {

        if (data instanceof QuakeData) {
            QuakeData qd = (QuakeData) data;

            mags.clear();

            mags = HttpGateway.getMagnitudesForQuake(qd.getQuakeID());
            System.out.println(mags);

            masterTable.setValueAt(qd.getCatalogName(), 0, 0);

            masterTable.setValueAt(new Long(qd.getQuakeID()), 1, 0);
            masterTable.setValueAt(TimeUtil.toText(qd.getDateTime()), 2, 0);
            masterTable.setValueAt(new Double(qd.getLatitude()), 3, 0);
            masterTable.setValueAt(new Double(qd.getLongitude()), 4, 0);
            masterTable.setValueAt(new Double(qd.getDepth()), 5, 0);
            masterTable.setValueAt(qd.getAgency(), 6, 0);
            masterTable.setValueAt(new Integer(qd.getFECode()), 7, 0);
            masterTable.setValueAt(new Boolean(qd.getHasMacroseismic()), 8, 0);
            masterTable.setValueAt(new Boolean(qd.getHasInfo()), 9, 0);

        }
    }

    public void createPanel() {

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.setBorder(BorderFactory
                .createTitledBorder(masterTableTitle));

        ListModel lm = new AbstractListModel() {
            
            private static final long serialVersionUID = -8375038626628948102L;

            public int getSize() {
                return masterTableHeaders.length;
            }

            public Object getElementAt(int index) {
                return masterTableHeaders[index];
            }
        };

        DefaultTableModel dm = new DefaultTableModel(lm.getSize(), 1);
        masterTable = new JTable(dm);

        JList rowHeader = new JList(lm);
        rowHeader.setFixedCellWidth(150);
        rowHeader.setFixedCellHeight(masterTable.getRowHeight());

        rowHeader.setCellRenderer(new RowHeaderRenderer(masterTable));
        rowHeader.setBackground(masterTable.getTableHeader().getBackground());

        JScrollPane scroll = new JScrollPane(masterTable);
        scroll.setRowHeaderView(rowHeader);

        centerPanel.add(scroll);

        detailsTable = new JTable(new DetailsTableModel());
        JScrollPane scrollDetails = new JScrollPane(detailsTable);
        detailsTable.setPreferredScrollableViewportSize(masterTable
                .getPreferredSize());

        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(scrollDetails);
        northPanel.setBorder(BorderFactory
                .createTitledBorder("Magnitudes associated with Earthquake"));

        add(centerPanel);
        add(northPanel);

        // We don`t want a column header
        masterTable.setTableHeader(null);

        validate();
    }

    class DetailsTableModel extends AbstractTableModel {
        
        private static final long serialVersionUID = -1666341321247106163L;

        public int getRowCount() {
            return mags.size();
        }

        public int getColumnCount() {
            return detailHeaders.length;
        }

        public String getColumnName(int col) {
            return detailHeaders[col];
        }

        public Object getValueAt(int row, int column) {

            switch (column) {
            case 0:
                return (new Long(((MagData) mags.get(row)).getMagnitudeID()));

            case 1:
                return (new Double(((MagData) mags.get(row)).getMagnitude()));

            case 2:
                return ((MagData) mags.get(row)).getMagnitudeType();

            case 3:
                return (new Boolean(((MagData) mags.get(row)).isCalculated()));

            case 4:
                return ((MagData) mags.get(row)).getMagnitudeAgency();
            }

            return null;
        }

    }

}
